package com.itboye.lottery.activity.user

import android.annotation.SuppressLint
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.TextView
import com.itboye.lottery.MainActivity
import com.itboye.lottery.R
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.bean.UserDataBean
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import com.itboye.lottery.utils.Const
import com.itboye.lottery.utils.SPUtils
import com.itboye.lottery.utils.TimeCount
import com.itboye.lottery.utils.Utils.emptyMsgShow
import kotlinx.android.synthetic.main.activity_edit_phone.*

/**
 * 修改手机或绑定手机公用
 * type:0(绑定手机)  type:1(修改手机)
 */
class EditPhoneActivity : BaseActivity(), ResponseCallback, TextWatcher {


    var timeCount: TimeCount? = null
    override fun success(o: Any, requestSuccessType: String) {
        when (requestSuccessType) {
            userPresenter!!.sendCode_SUCCESS -> {
                code = o.toString()
                ByAlert.alert(o)
            }
            userPresenter!!.bindPhone_SUCCESS -> {
                ByAlert.alert(o)//
                SPUtils.put(this, null, Const.PHONE, phone)
            }
            userPresenter!!.updatePhone_SUCCESS -> {
                ByAlert.alert(o)
                SPUtils.put(this, null, Const.PHONE, newPhone)
                finish()
            }
            userPresenter!!.WX_LOGIN_SUCCESS -> {
                ByAlert.alert(getString(R.string.login_success))
                val userDataBean = o as UserDataBean
                SPUtils.put(this, userDataBean)
                val intent = Intent(userPresenter!!.WX_LOGIN_SUCCESS)
                sendBroadcast(intent)
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }
            else -> {
            }
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        ByAlert.alert(msg)
    }

    var tv_edit_phone_sure: TextView? = null
    var userPresenter: UserPresenter? = null
    var tv_get_yzm: TextView? = null
    var code: String = ""
    var wxCode = ""
    override fun getLayoutId(): Int {
        return R.layout.activity_edit_phone
    }

    override fun initData() {
        timeCount=TimeCount(30000,1000,tv_get_yzm)
        userPresenter = UserPresenter(this)
        setBindOrUpdatePhone()
        initListener()
    }

    private fun initListener() {
        et_old_phone_value.addTextChangedListener(this)
        et_new_phone_value.addTextChangedListener(this)
        et_yzm_value.addTextChangedListener(this)
    }

    fun setButtonEnable() {
        when (type) {
            5 -> {
                //绑定手机判段一个编辑框+code
                if (!et_old_phone_value.text.toString().isEmpty() && !et_yzm_value.text.toString().isEmpty()) {
                    tv_edit_phone_sure!!.setBackgroundColor(resources.getColor(R.color.main_color))
                    tv_edit_phone_sure!!.isEnabled = true
                } else {
                    tv_edit_phone_sure!!.setBackgroundColor(resources.getColor(R.color.gray_898989))
                    tv_edit_phone_sure!!.isEnabled = false
                }
            }
            6 -> {
                //绑定手机判段两个编辑框+code
                //绑定手机判段一个编辑框+code
                if (!et_old_phone_value.text.toString().isEmpty() && !et_new_phone_value.text.toString().isEmpty() && !et_yzm_value.text.toString().isEmpty()) {
                    tv_edit_phone_sure!!.setBackgroundColor(resources.getColor(R.color.main_color))
                    tv_edit_phone_sure!!.isEnabled = true
                } else {
                    tv_edit_phone_sure!!.setBackgroundColor(resources.getColor(R.color.gray_898989))
                    tv_edit_phone_sure!!.isEnabled = false
                }
            }
            else -> {
            }
        }
    }

    private var type: Int = 0

    @SuppressLint("SetTextI18n")
    private fun setBindOrUpdatePhone() {
        type = intent.getIntExtra("type", 0)
        when (type) {
            5 -> {
                //绑定手机
                setTitle("绑定手机号")
                try {
                    wxCode = intent.getStringExtra("code")
                } catch (e: Exception) {

                }
//                tv_edit_info_tips.text = "您正在为${getSp(Const.NICKNAME)}完善信息"
                tv_old_phone.text = "手机号码"
                et_old_phone_value.hint = "请输入" + tv_old_phone.text
                rl_new_phone.visibility = View.GONE
//                tv_edit_info_tips.visibility = View.VISIBLE
                rl_yqm.visibility = View.VISIBLE
            }
            6 -> {
                //修改手机
                setTitle("修改手机号")
                tv_old_phone.text = "原手机号码"
                et_old_phone_value.hint = ("请输入" + tv_old_phone.text)
                rl_new_phone.visibility = View.VISIBLE
//                tv_edit_info_tips.visibility = View.GONE
                rl_yqm.visibility = View.GONE
            }
            else -> {
            }
        }
    }

    private var phone: String = ""
    var newPhone: String = ""

    private var invite_code: String = ""

    override fun onClick(p0: View?) {
        when (p0) {
            tv_edit_phone_sure -> {
                phone = et_old_phone_value.text.toString()
                newPhone = et_new_phone_value.text.toString()
                code = et_yzm_value.text.toString()
                when (type) {
                    5 -> {
                        invite_code = et_yqm_value.text.toString()
                        if (!emptyMsgShow(phone, "手机号码")) return
                        if (!emptyMsgShow(invite_code, "邀请码")) return
                        if (!emptyMsgShow(code, "验证码")) return
                        userPresenter!!.wxLogin(wxCode, phone, invite_code, code)
//                        userPresenter!!.bindPhone(phone, code)
                    }
                    6 -> {
                        if (!emptyMsgShow(phone, "原手机号码")) return
                        if (!emptyMsgShow(newPhone, "新手机号码")) return
                        if (!emptyMsgShow(code, "验证码")) return
                        userPresenter!!.updatePhone(newPhone, code)
                    }
                    else -> {
                    }
                }
            }
            tv_get_yzm -> {
                phone = et_old_phone_value.text.toString()
                newPhone = et_new_phone_value.text.toString()
                //获取验证码
                when (type) {
                    5 -> {
                        if (!emptyMsgShow(phone, "手机号码")) return
                        timeCount?.start()
                        userPresenter!!.sendCode(type, phone)
                    }
                    6 -> {
                        if (!emptyMsgShow(phone, "手机号码")) return
                        if (!emptyMsgShow(phone, "原手机号码")) return
                        if (!emptyMsgShow(newPhone, "新手机号码")) return
                        timeCount?.start()
                        userPresenter!!.sendCode(type, newPhone)
                    }
                    else -> {
                    }
                }
            }
            else -> {
            }
        }
    }

    override fun afterTextChanged(p0: Editable?) {
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        setButtonEnable()
    }

}
